package com.spr.gw.controller.admin;

import com.spr.gw.common.PageBean;
import com.spr.gw.common.ResultBean;
import com.spr.gw.entity.Category;
import com.spr.gw.entity.Message;
import com.spr.gw.entity.SystemConfig;
import com.spr.gw.entity.VO.MessageVo;
import com.spr.gw.service.CategoryService;
import com.spr.gw.service.MessageService;
import com.spr.gw.service.SinglePageService;
import com.spr.gw.service.SystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private SystemConfigService systemConfigService;

    @Autowired
    private SinglePageService singlePageService;

    @Autowired
    private MessageService messageSerivce;

    @Autowired
    private CategoryService categoryService;

    /**
     * 获得系统配置文件json格式
     * @return
     */
    @RequestMapping("systemParameter.json")
    public ResultBean systemConfig(){
        return systemConfigService.findSystemConfig();
    }

    /**
     * 更新系统配置
     * @param systemConfig
     * @return
     */
    @PostMapping("updateSystemParameter")
    public ResultBean updateSystemConfig(@RequestBody SystemConfig systemConfig){
        return systemConfigService.updateSystemConfig(systemConfig);
    }

    /**
     * 获得所有单页信息集合
     * @return
     */
    @RequestMapping("getSinglePageList.json")
    public ResultBean<List<Category>> findSinglePageList(){
        return singlePageService.findAll();
    }

    /**
     * 获得一个单页信息
     * @param categoryId
     * @return
     */
    @RequestMapping("getSinglePage.json/{categoryId}")
    public ResultBean<Category> findOneSinglePage(@PathVariable Integer categoryId){
        return singlePageService.findOne(categoryId);
    }

    /**
     * 更具是否有id来添加或者更新单页信息
     * @param category
     * @return
     */
    @RequestMapping("addOrUpdateSinglePage")
    public ResultBean<Category> addOrUpdate(@RequestBody Category category){
        return singlePageService.addOrUpdate(category);
    }

    /**
     * 删除单页
     * @param categoryId
     * @return
     */
    @RequestMapping("deleteSinglePage/{categoryId}")
    public ResultBean<Category> deleteSinglePage(@PathVariable Integer categoryId){
        return singlePageService.deleteOne(categoryId);
    }

    /**
     * 获得所有的消息(普通文章)
     * @param filterMessage
     * @return
     */
    @RequestMapping("getAllMessage.json")
    public PageBean<MessageVo> getAllMessage(MessageVo filterMessage){
        PageBean<MessageVo> messages = messageSerivce.findMessages(filterMessage);
        return messages;
    }
    /**
     * 根据categoryId获得消息
     * @param categoryId
     * @param pageNum
     * @return
     */
    @RequestMapping("getAllMessageByCategoryId/{categoryId}/{pageNum}")
    public PageBean<MessageVo> getAllMessageByCatId(@PathVariable("categoryId") Integer categoryId,
                                                    @PathVariable(value = "pageNum", required = false) Integer pageNum){
        PageBean<MessageVo> messages = messageSerivce.findMessagesByCategoryId(categoryId, pageNum);
        return messages;
    }

    /**
     * 添加消息或者有messageId更新消息
     * @param message
     * @return
     */
    @RequestMapping("addMessage")
    public ResultBean<Message> addMessage(@RequestBody Message message){
        return messageSerivce.insertMessage(message);
    }

    /**
     * 根据id删除消息
     * @param id
     * @return
     */
    @RequestMapping("/deleteMessage/{id}")
    public ResultBean<Message> messageDelete(@PathVariable("id") Integer id){
        return messageSerivce.messageDelete(id);
    }

    /**
     * 获得(X图片X)栏目所有信息通过type
     * @return
     */
    @RequestMapping("/getMessageInfoByType/{type}")
    public ResultBean<List<MessageVo>> getPictureMessage(@PathVariable("type") String type){
        return messageSerivce.findAllMessageByType(type);
    }

    /**
     * 根据类型id查询栏目
     * @param typeid
     * @return
     */
    @RequestMapping("/queryCategoryByType/{typeid}")
    public ResultBean<List<Category>> getCategoryByType(@PathVariable("typeid") String typeid){
        return categoryService.getCategoryByType(typeid);
    }
}
